package com.dg11185.nearshop.view;

import com.android.nearshop.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

/**
 * 自带加载机制的FrameLayout
 * 
 * @author wanliang527
 * @date 2014-5-8
 */
public class ViewWithLoading extends FrameLayout {

    public final static int FAIL = 0;
    public final static int SUCCESS = 1;

    private Context mContext;
    private Animation loadAnim;
    private ViewStub loadingView, loadFailedView;

    private ReloadEvent mReloadEvent;

    private boolean isLoading;

    public ViewWithLoading(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public ViewWithLoading(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ViewWithLoading(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        loadingView = new ViewStub(mContext);
        loadingView.setLayoutResource(R.layout.view_loading);
        this.addView(loadingView);
        ((FrameLayout.LayoutParams) loadingView.getLayoutParams()).gravity = Gravity.CENTER;

        loadFailedView = new ViewStub(mContext);
        loadFailedView.setLayoutResource(R.layout.view_load_failed);
        this.addView(loadFailedView);
        ((FrameLayout.LayoutParams) loadFailedView.getLayoutParams()).gravity = Gravity.CENTER;

    }

    public void startLoadingAnim() {
        if (loadAnim == null)
            loadAnim = AnimationUtils.loadAnimation(mContext,
                    R.anim.loading_dialog_anim);
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
            isLoading = true;
            findViewById(R.id.loading_image_v).startAnimation(loadAnim);
        }
    }

    /**
     * 请在调用startLoadingAnim()和stopLoadingAnim(int loadState)方法前调用该方法，用于设置重新加载事件
     * 重新加载事件的最后必须调用stopLoadingAnim(int loadState);
     * 
     * @param event
     */
    public void setReloadEvent(ReloadEvent event) {
        mReloadEvent = event;
    }

    public ReloadEvent getReloadEvent() {
        return mReloadEvent;
    }

    public void stopLoadingAnim(int loadState) {
        isLoading = false;
        switch (loadState) {
            case SUCCESS:
                hideLoadingView();
                break;

            case FAIL:
                hideLoadingView();
                showLoadFailedView();
                break;

            default:
                break;
        }
    }

    public boolean isLoading() {
        return isLoading;
    }

    private void hideLoadingView() {
        if (loadingView != null && loadingView.getVisibility() == View.VISIBLE) {
            loadingView.setVisibility(View.GONE);
            findViewById(R.id.loading_image_v).clearAnimation();
        }
    }

    private void showLoadFailedView() {
        if (loadFailedView != null) {
            loadFailedView.setVisibility(View.VISIBLE);
            findViewById(R.id.btn_reload).setOnClickListener(
                    new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mReloadEvent != null) {
                                hideLoadFailedView();
                                startLoadingAnim();
                                mReloadEvent.onReload();
                            }
                        }
                    });
        }
    }

    private void hideLoadFailedView() {
        if (loadFailedView != null
                && loadFailedView.getVisibility() == View.VISIBLE)
            loadFailedView.setVisibility(View.GONE);

    }

    /**
     * 重新加载事件接口
     * 
     * @author wanliang527
     * 
     */
    public interface ReloadEvent {
        public void onReload();
    }

}
